﻿/**
*
*一个用于解析IFC转化后的类parseIFC.js
*
*Daniel
*2017.12.28
*
*/
define(function (){
    var parseIFC = function (jsonObject){
        THREE.Geometry.call(this);
        this.type = 'parseIFC';
        this.parameters = {
            object: jsonObject,
        };
        this.parseObject = {
            normal: [],
            indices: [],
            vertices: [],
            uvs: [],
        };
        if (jsonObject !== undefined){

            var normal, indices, uv, vertices;
            var faces = jsonObject.Faces;
            var vector = jsonObject.Points;
           
            var dataObjects = this.parseObject;

            normal = dataObjects.normal;
            uv = dataObjects.uvs;
            indices = dataObjects.indices;
            vertices = dataObjects.vertices;
           
            
            var length = faces.length;
            for (var i = 0; i < length; i++){
                var objectData = faces[i];
                var normalsData = objectData.Normals;
                var lens = normalsData.length;
                for (var j = 0; j < lens; j++){
                    var x, y, z, uvsu, uvsv;
                    var nor = normalsData[j];
                    
                    var normals = nor;
                    x = normals["X"];
                    y = normals["Y"];
                    z = normals["Z"];
                    uvsu = normals["U"];
                    uvsv = normals["V"];
                    
                    
                    uv.push(uvsu, uvsv);
                    normal.push(x, y, z);
                }
                
                var index = objectData["Indices"];
                indices.unshift.apply(indices, index);
            }

            var len = vector.length;
            for (var i = 0; i < len; i++){
                var xi, yi, zi, point, vector3;
                point = vector[i];
                vector3 = new THREE.Vector3(point["X"], point["Y"], point["Z"]);
                xi = vector3.x;
                yi = vector3.y;
                zi = vector3.z;
                vertices.push(xi, yi, zi);
            }

        }
        this.fromBufferGeometry(new IFCBufferGeometry(this.parseObject));
        this.mergeVertices();
    };
    parseIFC.prototype = Object.create(THREE.Geometry.prototype);
    parseIFC.prototype.constructor = parseIFC;

    function IFCBufferGeometry(praseData){
        THREE.BufferGeometry.call(this);

        this.type = 'IFCBufferGeometry';
        if (praseData !== undefined){
            var indices, vertices, normals, uvs;
            indices = praseData.indices;
            vertices = praseData.vertices;
            normals = praseData.normal;
            uvs = praseData.uvs;
        }
        this.setIndex(indices);
        this.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
        this.addAttribute('uv', new THREE.Float32BufferAttribute(uvs, 2));
    }

    IFCBufferGeometry.prototype = Object.create(THREE.BufferGeometry.prototype);
    IFCBufferGeometry.prototype.constructor = IFCBufferGeometry;

    return parseIFC




});